import time

import execjs
import requests
import json
import hashlib
import random
# 需要安装pyexecjs,requests

# 不确定client_key是否发生变化472770f9e581cffb09349f422af57c5
client_key = "472770f9e581cffb09349f422af57c5d"#5f97ee1645b2b751a91d67a0

# 加载js文件
f = open("decode.js", 'r', encoding='utf-8')
htmlstr = f.read()
f.close()
ctx = execjs.compile(htmlstr)
session = requests.session()
ts = 1

# 对t，v格式的数据进行解码，获得原始格式
def decodemsg(t, v):
    return ctx.call('decode', t, v)

# 对原始数据进行编码获得t，v的格式
def encodemsg(t, v):
    return ctx.call('encode', t, v)

# 获取md5
def getmd5(a):
    m = hashlib.md5()
    m.update(a.encode("utf-8"))
    return m.hexdigest()

# 发送post指令，url为网址后面的指令部分，如/game/exchange，msg为map格式，只需要传parm内的数据
def commandpost(url, msg):
    global ts
    global uid
    global token
    headers = {
        "referer": "https://appservice.qq.com/1110797565/1.1.5/page-frame.html",
        "user-agent": "Mozilla/5.0 (Linux) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.185 Mobile Safari/537.36 QQ/8.5.5.5105 V1_AND_SQ_8.5.5_1630_YYB_D QQ/MiniApp",
        "content-type": "application/x-www-form-urlencoded",
        "accept-encoding": "gzip"}

    parm = json.dumps(msg).replace(" ", "")
    sign = getmd5(uid+token+str(ts)+parm+client_key) #计算sign

    datas1 = {"uid": uid, "ts": ts, "token": token, "params": parm, "sign": sign} #拼凑数据包
    t = random.randint(10000000, 99999999)
    v = encodemsg(t, json.dumps(datas1).replace(" ", "")) #将数据包转化为t，v格式

    res = session.post("https://rane.jwetech.com:9080/"+url,
                       't={}&v={}'.format(t, v), headers=headers)
    text = res.text
    js = json.loads(text)
    js = json.loads(decodemsg(js["t"], js["v"])) #解密返回数据
    print(ts, url, js)
    ts = ts+1
    return(js)

# post发送登录命令，datas可以留空
def loginpost(datas):
    headers = {
        "referer": "https://appservice.qq.com/1110797565/1.1.5/page-frame.html",
        "user-agent": "Mozilla/5.0 (Linux;) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.185 Mobile Safari/537.36 QQ/8.5.5.5105 V1_AND_SQ_8.5.5_1630_YYB_D QQ/MiniApp",
        "content-type": "application/x-www-form-urlencoded",
        "accept-encoding": "gzip"
    }
    # sign=getmd5(uid+str(ts)+parm+client_key)
    res = session.post(
        'https://rane.jwetech.com:9080//login/login', datas, headers=headers)
    text = res.text
    js = json.loads(text)
    js = json.loads(decodemsg(js["t"], js["v"]))
    print(js)

# 编解码测试
print(decodemsg(16421077, "b262ac856f59a182dfc9f4ab6ca18710420ea551"))
print(encodemsg(51907146, '{"errCode":0,"errMsg":"物品已被兑换完了"}'))

datas = "t=17992520&v=a2684f9c7a4a3a40e802244ac0b9d67578e1f7a0a74616531b8b5d4cd37d869deb6d14fc71d7772fa8ade837ace5a526a63d871f8fbdfbdbd240b745bc0835944a15e21a1b10a782699535812f331fc6856ac554ee21c15d620e1efa35373ac4b026d2f1bfd67646e5c2e2fa6fbd48677a3e33a6"#此处可以填login抓包的全部参数，用于登录，实测不登录，只要ts足够大就能响应请求
uid = "956AF5E3D4A81850075556F776FE7AD8" #个人的uid，右上角头像出可查956AF5E3D4A81850075556F776FE7AD8看1EA385D91D22DCED4AF95F3ECFEC2314
token = "212cba6d-98da-4411-b414-d728b779deab"
#loginpost(datas) #发送登陆数据

# gift id
# 超会月201 超会三月、六月、年 202 203 204
# 大会员月214 大会员年 215
#a=1
#while a<100:
#    commandpost("/login/nickname", {})
#    a+=100
#commandpost("/login/nickname", {})nickname
# dat={"id":214}
# commandpost("/game/exchange",dat)
# 兑换测试
a=1
while a<11:
    d = random.randint(130, 140)
    b = d*16+30
    dat={}
    commandpost("/game/fbStart",dat)
    dat={"score":b,"step":d}
    time.sleep(60)
    commandpost("/game/fbSingle",dat)
    time.sleep(1)
    commandpost("/game/fbSingleVideo",dat)

#单人模式刷分测试
# dat={}
# commandpost("/game/fbStart",dat)
# dat={"score":1560,"step":101}
# time.sleep(60)
# commandpost("/game/fbSingle",dat)
# commandpost("/game/fbSingleVideo",dat)
#pk
#fbSingleVideo